import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ExcelImport {
	
	private String inputFile;
	private CreateUser userImporter;

	public ExcelImport(){
		userImporter = new CreateUser();
	}
	public void setInputFile(String inputFile) {
		this.inputFile = inputFile;
	}
	
	public void read() throws IOException  {
		File inputWorkbook = new File(inputFile);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);
			// Loop over first 10 column and lines
			System.out.println(sheet.getRows() + " Row Anzahl");
			System.out.println(sheet.getColumns() + " Column Anzahl");
			for (int j = 5; j < sheet.getRows(); j++) {
				String vorname = null;
				String nachname = null;
				String mail = null;
				String id = null;
				for (int i = 0; i < sheet.getColumns(); i++) {
					Cell cell = sheet.getCell(i, j);
					if (!cell.getContents().equals("")){
						switch (i) {
							case 0: vorname = cell.getContents(); break;
							case 1: nachname = cell.getContents(); break;
							case 2: mail = cell.getContents(); break;
							case 4: id = cell.getContents(); break;
						}
					}
					else{
						System.out.println("Ende der Tabelle. Es wurden " + (j-5) + " User importiert");
						return;
					}
				}
				userImporter.newUser(vorname, nachname, mail, id);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		}
	}



	
	public static void main(String[] args) throws IOException {
		ExcelImport test = new ExcelImport();
		test.setInputFile("c:/TestUser.xls");
		test.read();
	}
}
